package me.zhengjie.modules.report.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.report.domain.ReportConfig;
import me.zhengjie.modules.report.service.ReportConfigService;
import me.zhengjie.modules.report.service.dto.ReportConfigQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author vivi207
* @date 2019-07-25
*/
@RestController
@RequestMapping("api")
public class ReportConfigController {

    @Autowired
    private ReportConfigService reportConfigService;

    @Log("查询ReportConfig")
    @GetMapping(value = "/reportConfig")
    @PreAuthorize("hasAnyRole('ADMIN','REPORTCONFIG_ALL','REPORTCONFIG_SELECT')")
    public ResponseEntity getReportConfigs(ReportConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(reportConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增ReportConfig")
    @PostMapping(value = "/reportConfig")
    @PreAuthorize("hasAnyRole('ADMIN','REPORTCONFIG_ALL','REPORTCONFIG_CREATE')")
    public ResponseEntity create(@Validated @RequestBody ReportConfig resources){
        return new ResponseEntity(reportConfigService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改ReportConfig")
    @PutMapping(value = "/reportConfig")
    @PreAuthorize("hasAnyRole('ADMIN','REPORTCONFIG_ALL','REPORTCONFIG_EDIT')")
    public ResponseEntity update(@Validated @RequestBody ReportConfig resources){
        reportConfigService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除ReportConfig")
    @DeleteMapping(value = "/reportConfig/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','REPORTCONFIG_ALL','REPORTCONFIG_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        reportConfigService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}